<html>
  <body>
    <h1>Calculator</h1>
	
    <p>(Project 1A: By Dana Sadgat) <br />
	Type an expression in the following box (e.g., 10.5+20*3/25).
	</p> 
	
	<form method="GET">
		<input type="text" name="expr">   
		<input type="submit" value="Calculate">
	</form>
	
	<ul>
		<li>Only numbers and +,-,* and / operators are allowed in the expression.</li>
		<li>The evaluation follows the standard operator precedence.</li>
		<li>The calculator does not support parentheses.</li>
		<li>The calculator handles invalid input "gracefully". It does not output PHP error messages.</li>
	</UL>

	<p> Here are some(but not limit to) reasonable test cases: </p>
	
	<ol>
		<li>A basic arithmetic operation: 3+4*5=23</li>
		<li>An expression with floating point or negative sign : -3.2+2*4-1/3 = 4.46666666667, 3+-2.1*2 = -1.2</li>
		<li>Some typos inside operation (e.g. alphabetic letter): Invalid input expression 2d4+1</li>
	</ol>
	
	
	<?php 
	$param = $_GET["expr"]; 
	$result = $param;

	$regexp = "#^(http://)?(?=[-]?[0-9]+('.'.[0-9]+)?)([+-/*](?=[-]?[0-9]+('.'.[0-9]+)?))*#";
	
	if($param != NULL){
		echo "<h2>Result</h2>";
		if (preg_match($regexp, $param)){
			if (@eval("return \$ans = $result ;")){
				echo $param . " = " . $ans;
			}else{
				echo "Invalid input expression " . $param;
			}
		}else{
			echo "Invalid input expression " . $param;
		}
	}
	?><br />

	</body>
</html>